/**
 * Filter for the columns of boolean type
 * 
 * @author remo.lemma@lu.unisi.ch
 */

package ch.unisi.inf.projects.java.uom.gui.table.item.filter;

import java.util.ArrayList;
import java.util.List;

import org.jdesktop.swingx.decorator.Filter;

public class BooleanFilter extends Filter
{
	private List<Integer> toPrevious;

	private String state;
		
	public BooleanFilter (final String state, final int col)
	{
		super(col);
		toPrevious = new ArrayList<Integer>();
		setState(state);
	}
	
	public void setState (final String state)
	{
		this.state = state;
		refresh();
	}
	
	public void init ()
	{
		toPrevious = new ArrayList<Integer>();
	}
	
	public void reset ()
	{
		toPrevious.clear();
		final int inputSize = getInputSize();
		fromPrevious = new int[inputSize];
		for (int i = 0; i < inputSize; i++)
			fromPrevious[i] = -1;
	}
	
	public void filter ()
	{
		final int inputSize = getInputSize();
		int current = 0;
		final boolean present;
		if (state.toUpperCase().equals("ITEM PRESENT"))
			present = true;
		else
			present = false;
		for (int i = 0; i < inputSize; i++) {
			if (!adapter.isTestable(getColumnIndex()))
				return;
			else if (state.toUpperCase().equals("ALL") || getInputValue(i, getColumnIndex()).equals(present) || (present == false && getInputValue(i, getColumnIndex()).equals(""))) {
				toPrevious.add(new Integer(i));
				fromPrevious[i] = current++;
			}	
		}
	}
	
	public int getSize()
	{
		return toPrevious.size();
	}
	
	protected int mapTowardModel (final int row)
	{
		return toPrevious.get(row);
	}
}
